package von.seiji.cn.imageT;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.URLUtil;
import org.junit.Test;
import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
import org.opencv.objdetect.Objdetect;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import static org.opencv.highgui.HighGui.imshow;
import static org.opencv.highgui.HighGui.waitKey;

public class 人脸检测 {


    static {

        URL url = ClassLoader.getSystemResource("data/opencv_java454.dll");
        System.load(url.getPath());
    }

    @Test
    public void saldfj() throws MalformedURLException {


        String imgPath = "C:\\Users\\Administrator\\Pictures\\微信图片_20211840.png";
        byte[] bytes1 = FileUtil.readBytes(imgPath);

        String imgUrl = "https://img1.baidu.com/it/u=2143642705,2896665897&fm=26&fmt=auto";
        byte[] bytes = IoUtil.readBytes(URLUtil.getStream(new URL(imgUrl)));
        Mat img = Imgcodecs.imdecode(new MatOfByte(bytes), Imgcodecs.IMREAD_COLOR);
        imshow("L0", img);
        System.out.println(img.cols() + "\t" + img.rows() + "\t" + img.width() + "\t" + img.height()) ;
//        Mat img = 读入图像
        Rect Roi = new Rect(new Point(10, 10), new Size(img.width()-20, img.height()-20));
        Mat image = img.clone().submat(Roi);//子图

//        Mat image = new Mat(img.clone(),Roi);
        URL xmlfileUrl = ClassLoader.getSystemResource("data/haarcascade_frontalface_alt2.xml");
        System.out.println(xmlfileUrl.getFile());
        System.out.println("new File(xmlfileUrl.getPath()).exists() = " + new File(xmlfileUrl.getPath()).exists());
        System.out.println(new File(xmlfileUrl.getPath()).getAbsolutePath());

        MatOfRect faceDetections = new MatOfRect();
        CascadeClassifier faceDetector = new CascadeClassifier(new File(xmlfileUrl.getPath()).getAbsolutePath());

//        faceDetector.detectMultiScale(image, faceDetections, 1.1, 2, 0 | Objdetect.CASCADE_FIND_BIGGEST_OBJECT, new Size(), new Size());
        faceDetector.detectMultiScale(image, faceDetections, 1.1, 2, 0 | Objdetect.CASCADE_SCALE_IMAGE, new Size(100, 100));
//        faceDetector.detectMultiScale(image, faceDetections);
        for (Rect rect : faceDetections.toArray()) {
            Imgproc.rectangle(image, rect.tl(), rect.br(), new Scalar(0, 0, 255), 1);
            Imgproc.putText(image, "by SeiJi", rect.tl(), Imgproc.FONT_HERSHEY_SCRIPT_SIMPLEX, Imgproc.CV_HOUGH_MULTI_SCALE, Scalar.all(150));
        }
        imshow("SL", image);

        Mat mat = new Mat();
        Imgproc.resize(image, mat, new Size(image.width()/2,image.height()/2));
        imshow("SL1", mat);

        waitKey(10000);



    }

    /**
     *
     * @param frame
     */
    //以下是人脸检测小函数
    private void detectAndDisplay(Mat frame) {
        MatOfRect faces = new MatOfRect();
        Mat grayFrame = new Mat();
        // convert the frame in gray scale
        Imgproc.cvtColor(frame, grayFrame, Imgproc.COLOR_BGR2GRAY);
        //  equalize the frame histogram to  improve the result
        Imgproc.equalizeHist(grayFrame, grayFrame);
        //  compute minimum face size (20%  of the frame height, in our case)
        int height = grayFrame.cols();

        int absoluteFaceSize = Math.round(height * 0.2f);


        String xmlfilePath = "haarcascade_frontalface_alt2.xml";
        MatOfRect faceDetections = new MatOfRect();
        CascadeClassifier faceDetector = new CascadeClassifier(xmlfilePath);
        //  detect faces
        faceDetector.detectMultiScale(grayFrame, faces, 1.1, 2, 0 | Objdetect.CASCADE_SCALE_IMAGE, new Size(absoluteFaceSize, absoluteFaceSize), new Size());
        //  each rectangle in faces is a  face: draw them!
        Rect[] facesArray = faces.toArray();
        for (Rect rect : facesArray) {
            Imgproc.rectangle(frame, rect.tl(), rect.br(), new Scalar(0, 255, 0), 3);
        }
    }


}
